﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.Finiance
{
    public partial class AgingAccountAdd : BasePage
    {
        private ArmCustomerAgeBll bll = new ArmCustomerAgeBll();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initForm();
            }
        }
        private void initForm()
        {
            if (Request.QueryString["ID"] != null && Request.QueryString["ID"] != "")
            {
                ArmCustomerAgeEntity entity = bll.Get<ArmCustomerAgeEntity>(new Guid(Request.QueryString["ID"].ToString()));
                txtMonth.Text = entity.Age.ToString();
                txtDateFrom.SelectedDate = entity.DateFrom;
                txtDateTo.SelectedDate = entity.DateTo;
                txtID.Value = Request.QueryString["ID"].ToString();
                listType.SelectedValue = entity.Type.ToString();
            }
            if (Request.QueryString["CustomerID"] != null && Request.QueryString["CustomerID"] != "")
            {
                txtCustomerID.Value = Request.QueryString["CustomerID"];
            }
        }

        private void save()
        {
            ArmCustomerAgeEntity entity = new ArmCustomerAgeEntity();
            if (txtID.Value != "")
            {
                entity.ID = new Guid(txtID.Value);
            }
            else
            {
                entity.ID = Guid.NewGuid();
                entity.Deleted = false;
            }
            entity.Age = System.Convert.ToInt32(txtMonth.Text.Trim());
            entity.DateFrom = txtDateFrom.SelectedDate.Value;
            entity.DateTo = txtDateTo.SelectedDate.Value;
            entity.CustomerID = new Guid(txtCustomerID.Value.Trim());
            entity.Type = System.Convert.ToInt32(listType.SelectedValue);
            bll.InsertOrUpdate(entity);
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            save();
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');window.close();", true);
        }
    }
}
